import { message } from 'antd';
import { getwarning } from './service';
export default {
  namespace: 'warnings',
  state: {
    warninglist: [],
    length: 10,
  },
  reducers: {
    update(state, { payload }) {
      return {
        ...state,
        warninglist: Array.from(payload),
      };
    },
    // updatelen(state, { payload }) {
    //   // console.log('length = ', state.length);
    //   // console.log(typeof (Array.from(payload)));
    //   let list = [...payload]
    //   console.log(typeof list);
    //   list = list.map((item) => {
    //     if (item.alarmId === 632) {
    //       item.tags.push(['new'])
    //       console.log('add success');
    //     }
    //     item.tags.push(['new'])
    //   })
    //   return {
    //     ...state,
    //     length: payload.length,
    //     warninglist: list
    //   }
    // }
  },
  effects: {
    *getwarning({ payload }, { call, put }) {
      const response = yield call(getwarning, payload);
      if (response.code === 0) {
        response.data = response.data.map((item) => {
          Object.assign(item, { tags: [] });
          // if (item.alarmId > length) {
          //   item.tags.push('new')
          // }
          return item;
        });
        // yield put({
        //   type: 'updatelen',
        //   payload: response.data
        // })
        yield put({
          type: 'update',
          payload: response.data,
        });

        console.log(response.data);
      } else {
        message.error(response.msg);
      }
    },
    // *download({ payload }, { call, put }) {
    //   const response = yield call(download, payload);
    //   console.log(response);
    // }
  },
};
